package com.shop.admin.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.dao.AllOrderDao;
import com.shop.admin.entity.AllOrder;
import com.shop.admin.entity.User;
import com.shop.admin.service.AllOrderService;
import com.shop.admin.service.ShopUserService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.vo.AdminErrandVo;
import com.shop.admin.vo.IndentVo;
import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamReader;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AllOrderServiceImpl extends ServiceImpl<AllOrderDao, AllOrder> implements AllOrderService {

    @Resource
    private AllOrderDao allOrderDao;

    @Resource
    private ShopUserService shopUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<AdminErrandVo> page = new Page<>(Long.parseLong(params.get("page").toString()),Long.valueOf(params.get("limit").toString()) );
        Page<AdminErrandVo> resPage = allOrderDao.queryJoinErrandOrderCondition(page,params);
//        List<AdminErrandVo> records = resPage.getRecords();
//        System.out.println(records);
//        System.out.println(records);
//        List<Integer> integers = records.stream().map(i -> i.getReceivingUserId()).collect(Collectors.toList());
//        integers.removeAll(Collections.singleton(null));
//        if (StringUtils.isEmpty(integers.toString())){
//            PageUtils pageUtils = new PageUtils(resPage); return pageUtils;
//        }
//        List<User> receivingUser = shopUserService.list(new QueryWrapper<User>().lambda().in(User::getUserId, integers));
//        System.out.println(receivingUser);
//        List<AdminErrandVo> adminErrandVos = records.stream().map(item -> {
//            AdminErrandVo adminErrandVo = new AdminErrandVo();
//            BeanUtils.copyProperties(item, adminErrandVo);
//            receivingUser.forEach(i -> {
//                if (item.getReceivingUserId()!=null&&item.getReceivingUserId().equals(i.getUserId().intValue())) {
//                    adminErrandVo.setRiderPhone(i.getPhonenumber());
//                }
//            });
//            return adminErrandVo;
//        }).collect(Collectors.toList());
//        if (!StringUtils.isEmpty(params.get("riderPhone").toString())) {
//            List<AdminErrandVo> riderPhoneErrand = new ArrayList<>();
//            for (AdminErrandVo record : records) {
//                if (record.getReceivingUserId()!=null&&record.getRiderPhone().equals(params.get("riderPhone")))
//                    riderPhoneErrand.add(record);
//            }
//            //List<AdminErrandVo> riderPhoneErrand = records.stream().filter(i -> i.getRiderPhone().equals(params.get("riderPhone"))).collect(Collectors.toList());
//            resPage.setRecords(riderPhoneErrand);
//        }

        PageUtils pageUtils = new PageUtils(resPage);
        return pageUtils;
    }
}
